package com.lq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lq.entity.Route;
import com.lq.mapper.RouteMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author luoqian
 * @date 2022/4/2 14:02
 *
 * @deprecated 航线管理控制器
 *
 * ----------- Springboot注解说明 ---------------
 * @CrossOrigin 允许跨域访问改接口
 * @RestController 控制器实例化
 * @Autowired 注入bean对象
 * @PostMapping 定义路由已经请求方式
 * @RequestParam 请求参数 -- 基本类型
 * @RequestBody 请求参数 -- 包装类型/实体对象等
 *
 * ------------ Swagger注解说明 -----------------
 * @Api 作用于类，用来在swagger上对一整个类做一个标题分类
 * @ApiOperation 作用于方法，用来标明方法（该接口）是用来做什么的
 * @ApiParam 作用于请求参数，用于标明请求参数的含义
 */
@CrossOrigin
@RestController
@Api(tags = {"航线管理控制层"})
public class RouteController {

    @Autowired
    private RouteMapper routeMapper;

    @PostMapping("/route/getPage")
    @ApiOperation(value = "分页获取航线信息", notes = "/route/getPage")
    public IPage<Route> getPageStudentInfo(@RequestParam @ApiParam(name = "pageNum", value = "pageNum", required = true) int pageNum, @RequestParam @ApiParam(name = "pageSize", value = "pageSzie", required = true) int pageSize, @RequestBody @ApiParam(name = "route", value = "route", required = true) Route route) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(!("").equals(route.getRouteNo())){
            queryWrapper.like("route_no",route.getRouteNo());
        }
        if (!("").equals(route.getStartCity())){
            queryWrapper.like("start_city",route.getStartCity());
        }
        if (!("").equals(route.getEndCity())){
            queryWrapper.like("end_city",route.getEndCity());
        }
        Page<Route> routePage = new Page<>(pageNum, pageSize);
        return routeMapper.selectPage(routePage, queryWrapper);
    }

    @PostMapping("/route/insert")
    @ApiOperation(value = "新增航线信息", notes = "/route/insert")
    public int insert(@RequestBody @ApiParam(name = "route", value = "route", required = true) Route route){
        return routeMapper.insert(route);
    }

    @PostMapping("/route/getList")
    @ApiOperation(value = "获取所有航线信息", notes = "/route/getList")
    public List<Route> getList(){
        return routeMapper.selectList(null);
    }

    @PostMapping("/route/update")
    @ApiOperation(value = "修改航线信息", notes = "/route/update")
    public int update(@RequestBody @ApiParam(name = "route", value = "route", required = true) Route route){
        return routeMapper.updateById(route);
    }

    @PostMapping("/route/delete")
    @ApiOperation(value = "批量删除航线信息", notes = "/route/delete")
    public int delete (@RequestParam @ApiParam(name = "ids",value = "1,2,3",required = true) String ids){
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return routeMapper.deleteBatchIds(list);
    }
}
